<template>
  <div class="container">
    <el-card>
      <el-form label-width="80px" :model="searchFormData">
        <el-row type="flex" justify="space-between" style="width: 100%; height: 35px">
          <el-col :span="5">
            <el-form-item label="关键字">
              <el-input placeholder="根据编号搜索" style="width: 415px" v-model="searchFormData.keyword"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-row type="flex" justify="end">
              <el-form-item>
                <el-button size="small" @click="searchFormData.keyword = ''">清除</el-button>
                <el-button type="primary" size="small" @click="search">搜索</el-button>
              </el-form-item>
            </el-row>
          </el-col>
        </el-row>
      </el-form>
      <el-row style="margin: 15px 0px">
        <el-alert :closable="false" :title="`数据一共 ${counts} 条`" type="info" show-icon></el-alert>
      </el-row>
      <el-row>
        <el-table :data="randomsList">
          <el-table-column label="编号" prop="id" width="220"></el-table-column>
          <el-table-column label="题型" width="80">
            <template #default="{ row }">
              {{ questionTypeList[row.questionType - 0 - 1] }}
            </template>
          </el-table-column>
          <el-table-column label="题型编号" width="220">
            <template #default="{ row }">
              <el-link v-for="item in row.questionIDs" :key="item.number" type="primary" @click="getDetail(item.id)">{{
                item.number
              }}</el-link>
            </template>
          </el-table-column>
          <el-table-column label="录入时间" prop="addTime" width="180"></el-table-column>
          <el-table-column label="答题时间(s)" prop="totalSeconds"></el-table-column>
          <el-table-column label="正确率(%)" prop="accuracyRate"></el-table-column>
          <el-table-column label="录入人" prop="userName"></el-table-column>
          <el-table-column label="操作" width="80">
            <template #default="{ row }">
              <el-button type="danger" plain icon="el-icon-delete" circle @click="delQuestion(row.id)"></el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-row>
      <!-- 分页 -->
      <el-row type="flex" justify="end">
        <el-pagination
          style="margin-top: 20px"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="query.page"
          :page-sizes="[20, 30, 50, 100]"
          :page-size="20"
          layout="prev, pager, next,sizes,jumper"
          background
          :total="counts"
        >
        </el-pagination>
      </el-row>
      <!-- 预览 -->

      <QuestionsPreview :value="list" :swit="swit" @changeShow="show"></QuestionsPreview>
    </el-card>
  </div>
</template>

<script>
import { randoms as randomsListAPI, removeRandoms, detail } from '@/api/hmmm/questions.js'
import QuestionsPreview from '../components/questions-preview.vue'
export default {
  data () {
    return {
      query: {
        // 查询参数
        page: 1,
        pagesize: 20
      },
      counts: 0, // 总条数
      randomsList: [], // table数据
      searchFormData: {
        // 搜索表单
        keyword: ''
      },
      // 题型数据
      questionTypeList: ['单选', '多选', '简答'],
      // 预览控制
      swit: false,
      // 数据
      list: {}
    }
  },

  created () {
    this.getQuestionsList() // 调用-获取题组列表
  },
  components: {
    QuestionsPreview
  },
  methods: {
    // 监听 子组件弹窗关闭后触发，有子组件调用
    show (data) {
      if (data === 'false') {
        this.swit = false
      } else {
        this.swit = true
      }
    },
    // 页面变化
    handleSizeChange (size) {
      this.query.pagesize = size
      this.getQuestionsList()
    },
    // 当前页变化
    handleCurrentChange (page) {
      this.query.page = page
      this.getQuestionsList()
    },
    // 获取题组列表
    async getQuestionsList () {
      const { data } = await randomsListAPI({ ...this.query })
      this.counts = data.counts
      this.randomsList = data.items
    },
    // 删除题组
    delQuestion (id) {
      this.$confirm('此操作将永久删除该题组, 是否继续?', '提示', { type: 'warning' })
        .then(async () => {
          if (this.query.page > 1 && this.randomsList.length === 1) this.query.page-- // 分页优化
          await removeRandoms({ id })
          this.$message.success('删除成功')
          this.getQuestionsList()
        })
        .catch(() => {})
    },
    // 搜索
    async search () {
      const { data } = await randomsListAPI({ ...this.query, keyword: this.searchFormData.keyword })
      this.counts = data.counts
      this.randomsList = data.items
    },
    // 点击链接
    async getDetail (id) {
      this.swit = true
      const { data } = await detail({ id, isNext: false })
      this.list = data
    }
  }
}
</script>

<style scoped lang="scss">
.container {
  padding: 15px;
  .el-form-item {
    margin-bottom: 15px;
  }
}
</style>
